<?PHP
/**
 * AES加密、解密类
 *
 * @author 用法：
 *         <pre>
 *         // 实例化类
 *         // 参数$_bit：格式，支持256、192、128，默认为128字节的
 *         // 参数$_type：加密/解密方式，支持cfb、cbc、nofb、ofb、stream、ecb，默认为ecb
 *         // 参数$_key：密钥，默认为abcdefghijuklmno
 *         $tcaes = new TCAES();
 *         $string = 'laohu';
 *         // 加密
 *         $encodeString = $tcaes->encode($string);
 *         // 解密
 *         $decodeString = $tcaes->decode($encodeString);
 *         </pre>
 */
namespace encrypt;

class OpensslAES
{
    protected $method;
    protected $secret_key;
    protected $iv;
    protected $options;

    public function __construct($key, $method = 'AES-128-ECB', $iv = '', $options = 0)
    {
        $this->secret_key = $key;
        $this->method = $method;
        $this->iv = $iv;
        $this->options = $options;
    }

    public function encrypt($data)
    {
        return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }

    public function decrypt($data)
    {
        return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
    }
}